<?php defined('SYSPATH') or die('No direct script access.');

	class Controller_MedicinePatient extends Controller{
		
		public function action_index(){
			$id_patient = $this->request->param("id");
					
			$parameters = array();
			$parameters["id_patient"] = $id_patient;
			$parameters["medicines"] = Helper_Service::factory("medicine")->getAll();
			
			$grid = new Jqgrid("medicinepatient", Helper_Service::factory("medicinepatient",$id_patient));
			
			$view = View::factory("page");
			$view->page_content = $grid->render("jqgrid/config/patient/medicine-patient",$parameters); 
						
			$this->request->response = $view->render();
		}
		
		public function action_list(){
			$id_patient = $this->request->param("id");
			$grid = new Jqgrid("medicinepatient", Helper_Service::factory("medicinepatient",$id_patient));
			
			$criteria = new Jqgrid_Criteria();
			$criteria->page = $_POST['page'];
			$criteria->limit = $_POST['rows'];
			$criteria->sidx = $_POST['sidx'];
			$criteria->sord = $_POST['sord'];
			
			$this->request->response = $grid->getList($criteria);
		
		}
		
		public function action_save(){
			$id_patient = $this->request->param("id");
			$grid = new Jqgrid("medicinepatient", Helper_Service::factory("medicinepatient",$id_patient));
			
			if (($_POST["oper"] == "add") || ($_POST["oper"] == "edit") ){ 
				$patient_age = Helper_Service::factory("patient",$id_patient)->getAge();
				$medication_since = $_POST["age_at_start"];
				$_POST["age_at_start"] = $patient_age - $medication_since;
			}
			
			
			
			$this->request->response = $grid->save($_POST['oper'],$_POST);
		}
	
	}



// End MedicinePatient 
?>
